 function [GG,RR,C,eu,SDX,ZZ,initss,ssvec,flag,ssnames,stanames,shonames]=modDebugFacThree(parest,~,~,calval,posest,poscal)
% =========================================================================
if nargout < 11 
    flag_repstanames=0; 
else 
    flag_repstanames=1; 
end 

flag.ssok=1;
flag.solver=1; 
eu=[0;0];
SDX=[]; 
ZZ=[]; 
GG=[]; 
RR=[]; 

initss=[]; 
ssnames=[]; 
ssvec  =[]; 
% =========================================================================
%% *PART 1. Declare All Variables in the Model Solution $$ S_{t} $$* 

%__________________________________________________________________________
% 1.a Endogenous Variables 
yOne  =1;            
yTwo  =2;            
yThree=3;            
uOne=4;           
uTwo   =5;           
uThree =6;         
fac      =7;            
facLagOne=8;            

NY=8; 

% =========================================================================
%% *PART 2. Declare Innovations to the exogenous variables $$ \eta_{t} $$* 

NX=4; 
%__________________________________________________________________________
% 2.a Structural Disturbances 
epsOne=1;           % MP
epsTwo=2;           % Technology 
epsThree=3;           % Public spending
nu=4;         % Investment     

% =========================================================================
%% *PART 3. Declare Parameters* 
ncof=        15;    % Number of coefficients 
numpar=      ncof;  % Equal to Parameters, distinction irrelevant here 
if nargin < 4 || isempty( calval ) 
    param=parest  ; 
else
    param=zeros(numpar,1); 
    param(posest)=parest; 
    param(poscal)=calval; 
end; 
%__________________________________________________________________________
% 3.1 Parameters for all but multiple prices, GDP and I deflators 
bOne    =param(1); 
bTwo=param(2); 
bThree=param(3); 
phiOne=param(4); 
phiTwo=param(5); 
phiThree=param(6); 
sigOne=param(7); 
sigTwo=param(8); 
sigThree=param(9); 
rhoOne=param(10); 
rhoTwo=param(11);  
sigFactor=param(12); 
yMeanOne=param(13); 
yMeanTwo=param(14); 
yMeanThree=param(15); 


AA=zeros(2); 
AA(1,:)=[rhoOne rhoTwo]; 
AA(2,1)=1; 
if max( abs(eig(AA)) ) > 0.9999 
    return 
end 

%==========================================================================
%% *PART 5. Declare System Matrices* 
% Model to be solved is written as 
% $$ A_{0}Y_{t+1} + A_{1}Y_{t}=A_{2}Y_{t-1} + PSI \eta_{t} $$  
%A0  = eye(NY) ;
GG  = zeros(NY,NY) ;
RR  = zeros(NY,NX) ;%% Eq 1, Output (y) from production function 

%%
GG(yOne,fac)=bOne*rhoOne;
GG(yOne,facLagOne)=bOne*rhoTwo;
RR(yOne,nu       )=bOne; 

GG(yOne,uOne     )=phiOne; 
RR(yOne,epsOne   )=1; 

%%
GG(uOne,uOne     )=phiOne; 
RR(uOne,epsOne   )=1; 

%%
GG(yTwo,fac)=bTwo*rhoOne;
GG(yTwo,facLagOne)=bTwo*rhoTwo;
RR(yTwo,nu       )=bTwo; 

GG(yTwo,uTwo     )=phiTwo; 
RR(yTwo,epsTwo   )=1; 

%% 
GG(uTwo,uTwo     )=phiTwo; 
RR(uTwo,epsTwo   )=1; 

%%
GG(yThree,fac)      =bThree*rhoOne;
GG(yThree,facLagOne)=bThree*rhoTwo;
RR(yThree,nu   )    =bThree; 

GG(yThree,uThree   )=phiThree; 
RR(yThree,epsThree )=1; 

%% 
GG(uThree,uThree   )=phiThree; 
RR(uThree,epsThree )=1; 

%% 
GG(fac,fac)=rhoOne; 
GG(fac,facLagOne)=rhoTwo; 

RR(fac,nu       )=1; 

%% 
GG(facLagOne,fac)=1; 


SDX=diag([sigOne;sigTwo;sigThree;sigFactor]);
if size(SDX,1)~=NX;error('Dimensions of errors and SDX do not match');end

if max(abs(eig(GG))) > 0.99999
    return
end
eu=[1;1];

ZZ=zeros(3,NY); 
ZZ(1,yOne)=1; 
ZZ(2,yTwo)=1; 
ZZ(3,yThree)=1; 


%% *PART 12: Declare C, Matrix of Constants*
C=zeros(NY,1); 
C(1:3)=[yMeanOne;yMeanTwo;yMeanThree]; 

if flag_repstanames==0; 
    return 
end 
%________________________________________________________
%% 16.1 STANAMES: Cell with Names of ALL states 
stanames={'yOne','yTwo','yThree','uOne','uTwo','uThree','Fac','FacOne'};

%% 16.2 STSHOCKS: Cell with Names of Exogenous Process in STANAMES 
%       Need not match all shocks, e.g. if some are i.i.d.
% *Note:* Technically not true for the policy signals, for which looking
% only at the cummulative signals. Hence isolate policy innovations rather
% than policy states.
shonames={'epsOne','epsTwo','epsThree','nu'}; 
stshocks={'uOne','uTwo','uThree','Fac'}; 

%% 16.4 Quick check dimensions 
if length(stanames)~=size(GG,1)
    error('Mistmatch STANAMES and NY');
end
if length(stshocks)~=size(SDX,1)
    warning('Mistmatch STSHOCKS and NX');
end
flag.shInStateNames=stshocks; 
flag.shInStatesPos =cellposition(flag.shInStateNames,stanames); 
if length(shonames)~=size(SDX,1)
    error('Mistmatch SHONAMES and NX');
end
